package com.zhijian.medical.controller;

import java.util.*;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.zhijian.entity.PageVo;
import com.zhijian.medical.entity.vo.request.*;
import com.zhijian.medical.entity.vo.response.RoutineQcPathologyVo;
import com.zhijian.medical.entity.vo.response.RoutineStepRecordVo;
import com.zhijian.medical.entity.vo.response.TotalCommonPhrasesVo;
import com.zhijian.medical.service.CommonPhrasesService;
import com.zhijian.medical.service.RoutineStepRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.zhijian.response.annotation.Response;
import com.zhijian.util.page.Page;

import com.zhijian.medical.entity.po.RoutineQc;
import com.zhijian.medical.service.RoutineQcService;
import com.zhijian.medical.entity.vo.response.RoutineQcVo;

import lombok.extern.slf4j.Slf4j;
import java.util.List;

/**
 * 常规病理-取材 控制层
 * 
 * @author auto
 * @version 1.0
 * @date 2024-07-24 19:18:37
 */
@Slf4j
@Controller
@Api(tags = "常规病理-取材")
public class RoutineQcController {

	@Autowired
    private RoutineQcService service;

    @Autowired
    private CommonPhrasesService commonPhrasesService;

    @Autowired
    private RoutineStepRecordService recordService;

    /**
     * 新增
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/common/phrases")
    @ApiOperation("新增-常用语")
    public boolean insertCommon(@RequestBody CommonPhrasesSaveOrUpdateVo param) {
        log.info("{} - {}| insert info", this.getClass().getSimpleName(), param);
        return commonPhrasesService.save(param);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Response
    @DeleteMapping("/common/phrases/{id}")
    @ApiOperation("删除-常用语")
    public boolean deleteCommon(@PathVariable("id") Long id) {
        log.info("{} - {}| delete info", this.getClass().getSimpleName(), id);
        commonPhrasesService.deleteById(id);
        return true;
    }

    /**
     * 修改
     *
     * @param id
     * @param param
     * @return
     */
    @Response
    @PutMapping("/common/phrases/{id}")
    @ApiOperation("修改-常用语")
    public boolean updateCommon(@PathVariable("id") Long id, @RequestBody CommonPhrasesSaveOrUpdateVo param) {
        log.info("{} - {}| update info, {}", this.getClass().getSimpleName(), id, param);
        param.setId(id);
        return commonPhrasesService.modify(param);
    }

    /**
     * 常用语层级结构
     *
     * @return
     */
    @Response
    @GetMapping("/common/phrases")
    @ApiOperation("常用语层级结构返回")
    public List<TotalCommonPhrasesVo> selectByListCommon() {
        log.info("{} | query info", this.getClass().getSimpleName());
        return commonPhrasesService.selectAllPhrases();
    }

    /**
     * 全部流程节点
     *
     * @return
     */
    @Response
    @GetMapping("/qc/step/record/{pathologyNumber}")
    @ApiOperation("全部流程节点")
    public List<RoutineStepRecordVo> selectByStrpRecord(@ApiParam(name = "pathologyNumber", value = "病理号") @PathVariable("pathologyNumber") String pathologyNumber) {
        log.info("{} | query info", this.getClass().getSimpleName());
        return recordService.selectList(RoutineStepRecordSearchVo.builder().conventionPathologyNumber(pathologyNumber).build());
    }
    /**
     * 新增
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/routine/qc")
    @ApiOperation("新增")
    public boolean insert(@RequestBody RoutineQcSaveOrUpdateVo param) {
        log.info("{} - {}| insert info", this.getClass().getSimpleName(), param);
        return service.save(param);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Response
    @DeleteMapping("/routine/qc/{id}")
    @ApiOperation("删除")
    public boolean delete(@PathVariable("id") Long id) {
        log.info("{} - {}| delete info", this.getClass().getSimpleName(), id);
        service.deleteById(id);
        return true;
    }

    /**
     * 修改
     *
     * @param id
     * @param param
     * @return
     */
    @Response
    @PutMapping("/routine/qc/{id}")
    @ApiOperation("修改")
    public boolean update(@PathVariable("id") Long id, @RequestBody RoutineQcSaveOrUpdateVo param) {
        log.info("{} - {}| update info, {}", this.getClass().getSimpleName(), id, param);
        param.setId(id);
        return service.modify(param);
    }

    /**
     * 根据id查询
     *
     * @param pathologyNumber
     * @return
     */
    @Response
    @GetMapping("/routine/qc/{pathologyNumber}")
    @ApiOperation("根据病理号查询")
    public RoutineQcVo selectById(@PathVariable("pathologyNumber") String pathologyNumber) {
        log.info("{} - {}| query info", this.getClass().getSimpleName(), pathologyNumber);
        return service.selectVoByNums(pathologyNumber);
    }

    /**
     * 分页查询
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/routine/pathology/info")
    @ApiOperation("按条件分页查询--病理信息")
    public Page<RoutineQcPathologyVo> selectByCondition(@RequestBody(required = false) RoutineQcPathologySearchVo param) {
        if (param == null) {
            param = RoutineQcPathologySearchVo.builder().build();
        }
        log.info("{} - {}| query info", this.getClass().getSimpleName(), param);
        return service.selectPagePathology(param);
    }

    @Response
    @PostMapping("/routine/pathology/list")
    @ApiOperation("按条件列表查询--病理信息")
    public List<RoutineQcPathologyVo> selectList(Integer type, @RequestBody(required = false) RoutineQcPathologySearchVo param) {
        if (param == null) {
            param = RoutineQcPathologySearchVo.builder().build();
        }
        if (type != null){
            param.setPatientHandleType(type);
        }else {
            param.setPatientHandleType(0);
        }
        log.info("{} - {}| query info", this.getClass().getSimpleName(), param);
        return service.selectListPathology(param);
    }

    /**
     * 分页查询
     *
     * @return
     */
    @Response
    @PostMapping("/routine/pathology/dayinfo")
    @ApiOperation("按日期 分页查询--取材目录")
    public Page<RoutineQcPathologyVo> selectByDay(PageVo pageVo) {
        log.info("{} - | query info", this.getClass().getSimpleName());
        return service.selectPageDay(pageVo);
    }

    @Response
    @PostMapping("/routine/pathology/daylist")
    @ApiOperation("按日期 列表查询--病理信息")
    public List<RoutineQcPathologyVo> selectListDay(Integer type) {
        log.info("{} -| query info", this.getClass().getSimpleName());
        // 获取今天的开始时间
        DateTime dateTime = DateUtil.beginOfDay(new Date());
        String startTime = DateUtil.formatDateTime(dateTime);
        RoutineQcPathologySearchVo searchVo = new RoutineQcPathologySearchVo();
        searchVo.setStartTime(startTime);
        if (type != null){
            searchVo.setPatientType(type);
        }else {
            searchVo.setPatientType(0);
        }
        return service.selectPatholoyListDay(searchVo);
    }
}